package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisMessageConstant;
import com.itheima.mapper.OrderDao;
import com.itheima.pojo.*;
import com.itheima.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public OrderVo findById(Long id) {
        Order order=orderDao.findById(id);
        OrderVo orderVo=new OrderVo();
        BeanUtils.copyProperties(order,orderVo);
        orderVo.setMember(orderDao.findMember(order.getMemberId()));
        orderVo.setSetmeal(orderDao.findSetmeal(order.getSetmealId()));
        return orderVo;
    }

    @Override
    public String submitOrder(MemberDto memberDto) {
        /*判断验证码是否正确*/
        if (memberDto.getValidateCode()==redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_ORDER)){
            /*查询预约日预约状况*/
            OrderSetting orderSetting=orderDao.findByDate(memberDto.getOrderDate());
            /*判断预约日是否可以预约*/
            if (orderSetting.getNumber()!=0){
                /*判断预约日是否预约满*/
                if (orderSetting.getNumber()>=orderSetting.getReservations()){
                    /*根据身份证查询该用户订单*/
                    Order order=orderDao.findOrderByIdCard(memberDto.getIdCard());
                    /*判断是否在同一天预约同一套餐*/
                    if (order.getOrderDate()==memberDto.getOrderDate()&&order.getSetmealId()==memberDto.getSetmealId()){
                        /*判断是否为会员*/
                        if (orderDao.findByIdCard(memberDto.getIdCard())==null){
                            /*注册会员*/
                            MemberVo memberVo=new MemberVo();
                            BeanUtils.copyProperties(memberDto,memberVo);
                            Date date = new Date(); // 获取当前时间
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 指定目标格式
                            String formattedDate = sdf.format(date);
                            memberVo.setRegDate(formattedDate);
                            orderDao.insert(memberVo);
                            return MessageConstant.ORDER_SUCCESS;
                        }
                        /*更新预约人数*/
                        orderDao.updateNum(memberDto.getOrderDate());
                        return MessageConstant.ORDER_SUCCESS;
                    }else {
                        return MessageConstant.HAS_ORDERED;
                    }

                }else {
                    return MessageConstant.ORDER_FULL;
                }
            }else {
                return MessageConstant.SELECTED_DATE_CANNOT_ORDER;
            }
        }else {
            return MessageConstant.VALIDATECODE_ERROR;
        }

    }
}
